/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.memcached;

import com.meetup.memcached.MemcachedClient;
import com.meetup.memcached.SockIOPool;
import com.vng.zing.lib.Config;
import java.util.Date;
import org.apache.log4j.Logger;

/**
 *
 * @author kienpd
 */
public class CacheFactory {
private static  Logger logger = Logger.getLogger(CacheFactory.class);
    public static final String MEMCACHED_INSTANCE_SURVEY = "MESURVEY";
    public static final String MEMCACHED_KEY_SURVEY = "survey.survey.%d";
    public static final String MEMCACHED_KEY_USER_ID = "survey.user.id.%d";
    public static final String MEMCACHED_KEY_USER_NAME = "survey.user.name.%s";
   
    private static String[] memcachedServers;
    private static SockIOPool memcachedChatPool;

    public static void initalize() {

        InitializeConfig initCfg = new InitializeConfig();
        initCfg.initializeConfig();
    }

    public static class InitializeConfig {

        public void initializeConfig() {
            String s = Config.getParamStr("cached", "server");
            memcachedServers = s.split(",");
            memcachedChatPool = SockIOPool.getInstance(MEMCACHED_INSTANCE_SURVEY);
            memcachedChatPool.setServers(memcachedServers);
            memcachedChatPool.setInitConn(10);
            memcachedChatPool.setMinConn(10);
            memcachedChatPool.setMaxConn(1000);
            memcachedChatPool.setMaxIdle(1000 * 60 * 60 * 6);
            memcachedChatPool.setMaintSleep(0);
            memcachedChatPool.setNagle(false);
            memcachedChatPool.setSocketTO(300);
            memcachedChatPool.setSocketConnectTO(0);
            memcachedChatPool.initialize();
        }
    }
    // survey item
    public static void setSurveyMemCache(int idSurvey, Object item) {
        String key = String.format(MEMCACHED_KEY_SURVEY, idSurvey);
        setCache(key, item, 1 * 60 * 60 * 1000, MEMCACHED_INSTANCE_SURVEY);
    }

    public static Object getSurveyMemCache(int idSurvey) {
        String key = String.format(MEMCACHED_KEY_SURVEY, idSurvey);
        return (Object) getCache(key, MEMCACHED_INSTANCE_SURVEY);

    }
    public static void deleteSurveyMemCache(int idSurvey){
         String key = String.format(MEMCACHED_KEY_SURVEY, idSurvey);
         deleteCache(key, MEMCACHED_INSTANCE_SURVEY);
    }
    // user infor by id
    public static void setUserByIDMemCache(int idUser, Object item) {
        String key = String.format(MEMCACHED_KEY_USER_ID, idUser);
        setCache(key, item, 24* 60 * 60 * 1000, MEMCACHED_INSTANCE_SURVEY);
    }
    public static Object getUserByIDMemCache(int idUser){
         String key = String.format(MEMCACHED_KEY_USER_ID, idUser);
         return (Object)getCache(key, MEMCACHED_INSTANCE_SURVEY);
    }
    // user infor by name
    public static void setUserByNameMemCache(String name, Object item){
        String key = String.format(MEMCACHED_KEY_USER_NAME, name);
        setCache(key, item,  24* 60 * 60 * 1000, MEMCACHED_INSTANCE_SURVEY);
    }
    public static Object getUserByNameMemCache(String name){
          String key = String.format(MEMCACHED_KEY_USER_NAME, name);
        return  (Object)getCache(key, MEMCACHED_INSTANCE_SURVEY);
    
    }

    private static Object getCache(String key, String instance) {
        try {
            MemcachedClient mc = new MemcachedClient(instance);

            return (Object) mc.get(key);
        } catch (Exception ex) {
            logger.error(ex.toString());
            return null;
        }
    }

    private static void setCache(String key, Object value, int expiry, String instance) {
        try {
            MemcachedClient mc = new MemcachedClient(instance);
            mc.set(key, value, new Date(System.currentTimeMillis() + expiry));
        } catch (Exception ex) {
             logger.error(ex.toString());
        }
    }
    private static void deleteCache(String key,String instance){
            MemcachedClient mc = new MemcachedClient(instance);
            mc.delete(key);

    }

}
